<!-- pages/login/components/mobile.vue -->
<script setup lang="ts">
import { getCodeApi, loginByMobileApi } from '@/apis/user';
import { useUserStore } from '@/stores/user';
import { ref } from 'vue';

//是否开启倒计时
const showCountDown=ref(false)
// 表单ref
const formRef =ref(null)
const store = useUserStore()
//点击获取验证码
const getMobileCode = async ()=>{
  try {
    // 1.0 表单校验
    // @ts-ignore
    await formRef.value?.validateField(['mobile'])
    //调用接口获取验证码
    const res=await getCodeApi({mobile:formData.value.mobile,type:'login'})
    // 提醒用户
    uni.showToast({
      title:'发送验证码成功',
      icon:'success'
    })
    //定时器,省着太快了(上线就不需要了)
    setTimeout(() => {
      formData.value.code = res.code
    }, 1000)
    // 1.1 开始倒计时
    showCountDown.value = true
  } catch (error) {
    console.log(error);
  }
}
// 表单数据
const formData=ref({
  mobile:'',
  code:''
})
// 表单校验
const formRules = {
  mobile: {
    rules: [
      { required: true, errorMessage: '请填写手机号码' },
      { pattern: '^1[3-9][0-9]{9}$', errorMessage: '手机号码格式不正确' },
    ],
  },
  code: {
    rules: [
      { required: true, errorMessage: '请输入验证码' },
      { pattern: '^[0-9]{6}$', errorMessage: '验证码格式不正确' },
    ],
  },
}
// 2. 是否勾选同意用户协议
const isAgree = ref(false)
//点击登录
const onLogin=async()=>{
  try {
     // 整体校验
  // @ts-ignore
  await formRef.value.validate()
  // 判断用户是否勾选协议
  if (!isAgree.value) return uni.showToast({ title: '请勾选同意用户协议', icon: 'none' })
  // 调用接口
  const res =await loginByMobileApi(formData.value)
  //持久化存储token
  store.token = res.token
  // 提醒用户
  uni.showToast({title: '登录成功', icon: 'success'})
  // 跳转回我的
  uni.switchTab({
    url:'/pages/my/index'
  })
  } catch (error) {
    console.log(error)
  }
 
}

</script>

<template>
  <uni-forms class="login-form" ref="formRef" :model="formData" :rules="formRules">
    <uni-forms-item name="mobile">
      <uni-easyinput
        v-model="formData.mobile"
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
      />
    </uni-forms-item>
    <uni-forms-item name="code">
      <uni-easyinput
        v-model="formData.code"
        :input-border="false"
        :clearable="false"
        placeholder="请输入验证码"
        placeholder-style="color: #C3C3C5"
      />
      <text class="text-button" v-if="!showCountDown" @click="getMobileCode">获取验证码</text>
      <view  class="text-button" v-else>
        <!-- 倒计时组件 -->
        <uni-countdown
         :show-day="false" 
         :show-hour="false"
          :show-minute="false" 
          :second="59"
          @timeup="showCountDown=false"
        />秒后重新发送
      </view>
    </uni-forms-item>

    <view class="agreement">
      <radio :checked="isAgree" color="#16C2A3" @click="isAgree=!isAgree" />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="onLogin">登 录</button>
  </uni-forms>
</template>


<style lang="scss">
  @import './styles.scss';
</style>